package com.ziq.baselib.test.page

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import com.ziq.base.base.BaseActivity
import com.ziq.base.base.BaseFragment
import com.ziq.base.extension.binding
import com.ziq.base.view.HorizontalScrollViewTab
import com.ziq.baselib.R
import com.ziq.baselib.databinding.ActivityHorizontalScrollviewTabBinding
import com.ziq.baselib.databinding.FragmentHorizontalscrollviewtabViewpagerBinding


class HorizontalScrollViewTabActivity: BaseActivity() {

    private val binding : ActivityHorizontalScrollviewTabBinding by binding()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        var data:ArrayList<HorizontalScrollViewTab.ContentItem> = arrayListOf()
        data.add(HorizontalScrollViewTab.ContentItem { "推荐" })
        data.add(HorizontalScrollViewTab.ContentItem { "王者荣耀"})
        data.add(HorizontalScrollViewTab.ContentItem { "绝地求生"})
        data.add(HorizontalScrollViewTab.ContentItem { "LOL"})
        data.add(HorizontalScrollViewTab.ContentItem { "星秀"})
        data.add(HorizontalScrollViewTab.ContentItem { "吃鸡手游"})
        data.add(HorizontalScrollViewTab.ContentItem { "吃喝玩乐"})
        data.add(HorizontalScrollViewTab.ContentItem { "主机"})
        data.add(HorizontalScrollViewTab.ContentItem { "CF"})
        data.add(HorizontalScrollViewTab.ContentItem { "颜值"})
        data.add(HorizontalScrollViewTab.ContentItem { "二次元"})
        data.add(HorizontalScrollViewTab.ContentItem { "DNF"})
        data.add(HorizontalScrollViewTab.ContentItem { "暴雪"})
        data.add(HorizontalScrollViewTab.ContentItem { "我的世界"})


        binding.HorizontalScrollViewTab.addData(data)
        var adapter: MyViewPagerAdapter = MyViewPagerAdapter(data, supportFragmentManager);
        binding.viewpager.adapter = adapter
        binding.HorizontalScrollViewTab.setOnHorizontalNavigationSelectListener {
            binding.viewpager.setCurrentItem(it, false)
        }
        binding.viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {

            var isScrolling: Boolean = false
            var tempPosition: Int = 0;
            var currentPostion: Int = 0;
            override fun onPageScrollStateChanged(state: Int) {
                isScrolling = state == ViewPager.SCROLL_STATE_DRAGGING
                Log.d("ziq", String.format("onPageScrollStateChanged %d  ", state) )
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                if(isScrolling){
                    tempPosition = position
                    currentPostion = binding.viewpager.currentItem
                }
                if(tempPosition == position){
                    if(currentPostion == 0){
                        binding.HorizontalScrollViewTab.setPositionChange(currentPostion, 1, positionOffset)
                        Log.e("ziq", String.format("111 from = %d to = %d  positionOffset = %f", currentPostion, 1, positionOffset) )
                    }
//                    else if(currentPostion == mViewpager.adapter!!.count - 1) {
//                        mHorizontalScrollViewTab.setPositionChange(currentPostion, 1, positionOffset)
//                    }
                    else if(currentPostion > position){
                        //左滑
                        binding.HorizontalScrollViewTab.setPositionChange(currentPostion, currentPostion - 1, 1 - positionOffset)
                        Log.e("ziq", String.format("222 from = %d to = %d  positionOffset = %f", currentPostion, currentPostion - 1, 1 - positionOffset) )
                    }else{
                        //右滑
                        binding.HorizontalScrollViewTab.setPositionChange(currentPostion, currentPostion + 1, positionOffset)
                        Log.e("ziq", String.format("333 from = %d to = %d  positionOffset = %f", currentPostion, currentPostion + 1, positionOffset) )
                    }
                }
            }

            override fun onPageSelected(position: Int) {
                binding.HorizontalScrollViewTab.setPosition(position)
            }
        })
    }


    class MyViewPagerAdapter(var data:ArrayList<HorizontalScrollViewTab.ContentItem>, var fm: FragmentManager): FragmentStatePagerAdapter(fm){

        override fun getCount(): Int {
            return data.size
        }

        override fun getItem(position: Int): Fragment {
            return MyFragment.getInstance(data.get(position).title)
        }

    }

    class MyFragment: BaseFragment(R.layout.fragment_horizontalscrollviewtab_viewpager) {

        companion object {// 包裹范围内 属于静态方法
            fun getInstance(label: String): MyFragment {
                var fragment: MyFragment = MyFragment()
                var bundle: Bundle = Bundle()
                bundle.putString("label", label)
                fragment.arguments = bundle
                return fragment
            }
        }

        private val binding : FragmentHorizontalscrollviewtabViewpagerBinding? by binding()

        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            binding?.label?.text = arguments?.getString("label")
        }

    }

}